<!DOCTYPE html>












  


<html class="theme-next pisces use-motion" lang="zh-CN">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">












  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=7.2.0">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=7.2.0">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=7.2.0">


  <link rel="mask-icon" href="/images/logo.svg?v=7.2.0" color="#222">






<link rel="stylesheet" href="/css/main.css?v=7.2.0">






<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css?v=4.7.0">






  
  
  <link rel="stylesheet" href="/lib/fancybox/source/jquery.fancybox.css">



<script id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '7.2.0',
    sidebar: {"position":"left","display":"post","offset":12,"onmobile":false},
    back2top: {"enable":true,"sidebar":false,"scrollpercent":true},
    copycode: {"enable":true,"show_result":true,"style":null},
    fancybox: true,
    mediumzoom: false,
    lazyload: false,
    pangu: false,
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    },
    localsearch: {"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},
    search: {
      root: '/',
      path: 'search.xml'
    },
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    translation: {
      copy_button: '复制',
      copy_success: '复制成功',
      copy_failure: '复制失败'
    }
  };
</script>

  <meta name="description" content="童年中的趣事是最最珍贵的。小时候，我们因为幼小无知而闹出了许多笑话，打开记忆的匣门，童年趣事便接踵而来。 做梦找到厕所，醒来尿床了 小时候穿着裆裤时，跟着小女孩跳皮筋 撒尿和泥，还玩的不亦乐乎 纠结自己要上清华还是北大，长大后发现想多了">
<meta name="keywords" content="微信小程序">
<meta property="og:type" content="article">
<meta property="og:title" content="虎事小程序">
<meta property="og:url" content="https://blog.youngzk.com/posts/88e91fb0/index.html">
<meta property="og:site_name" content="Muniz Blog">
<meta property="og:description" content="童年中的趣事是最最珍贵的。小时候，我们因为幼小无知而闹出了许多笑话，打开记忆的匣门，童年趣事便接踵而来。 做梦找到厕所，醒来尿床了 小时候穿着裆裤时，跟着小女孩跳皮筋 撒尿和泥，还玩的不亦乐乎 纠结自己要上清华还是北大，长大后发现想多了">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="https://blog.youngzk.com/posts/88e91fb0/1.png">
<meta property="og:image" content="https://blog.youngzk.com/posts/88e91fb0/2.png">
<meta property="og:image" content="https://blog.youngzk.com/posts/88e91fb0/3.png">
<meta property="og:image" content="https://blog.youngzk.com/posts/88e91fb0/4.png">
<meta property="og:image" content="https://blog.youngzk.com/posts/88e91fb0/5.png">
<meta property="og:updated_time" content="2019-07-28T05:22:54.719Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="虎事小程序">
<meta name="twitter:description" content="童年中的趣事是最最珍贵的。小时候，我们因为幼小无知而闹出了许多笑话，打开记忆的匣门，童年趣事便接踵而来。 做梦找到厕所，醒来尿床了 小时候穿着裆裤时，跟着小女孩跳皮筋 撒尿和泥，还玩的不亦乐乎 纠结自己要上清华还是北大，长大后发现想多了">
<meta name="twitter:image" content="https://blog.youngzk.com/posts/88e91fb0/1.png">





  
  
  <link rel="canonical" href="https://blog.youngzk.com/posts/88e91fb0/">



<script id="page.configurations">
  CONFIG.page = {
    sidebar: "",
  };
</script>

  
  <title>虎事小程序 | Muniz Blog</title>
  






  <script>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?502753d7b083822cd84ffc61132a5ac4";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>







  <noscript>
  <style>
  .use-motion .motion-element,
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-title { opacity: initial; }

  .use-motion .logo,
  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Muniz Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
    
    
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>



<nav class="site-nav">
  
    <ul id="menu" class="menu">
      
        
        
        
          
          <li class="menu-item menu-item-home">

    
    
      
    

    

    <a href="/" rel="section"><i class="menu-item-icon fa fa-fw fa-home"></i> <br>首页</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-books">

    
    
      
    

    

    <a href="/books" rel="section"><i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br>阅读</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-tags">

    
    
      
    

    

    <a href="/tags/" rel="section"><i class="menu-item-icon fa fa-fw fa-tags"></i> <br>标签</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-categories">

    
    
      
    

    

    <a href="/categories/" rel="section"><i class="menu-item-icon fa fa-fw fa-th"></i> <br>分类</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-archives">

    
    
      
    

    

    <a href="/archives/" rel="section"><i class="menu-item-icon fa fa-fw fa-archive"></i> <br>归档</a>

  </li>

      
      
        <li class="menu-item menu-item-search">
          <a href="javascript:;" class="popup-trigger">
          
            <i class="menu-item-icon fa fa-search fa-fw"></i> <br>搜索</a>
        </li>
      
    </ul>
  

  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off" placeholder="搜索..." spellcheck="false" type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



</div>
    </header>

    


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          
            

          
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
      

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://blog.youngzk.com/posts/88e91fb0/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="修心">
      <meta itemprop="description" content="修不被别人影响的心，修不被自己控制的心，修不被别人诱惑的心。">
      <meta itemprop="image" content="/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Muniz Blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">虎事小程序

              
            
          </h1>
        

        <div class="post-meta">

          
          
          

          
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2018-08-19 17:45:23" itemprop="dateCreated datePublished" datetime="2018-08-19T17:45:23+08:00">2018-08-19</time>
            </span>
          

          
            

            
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                
                  <span class="post-meta-item-text">更新于</span>
                
                <time title="修改时间：2019-07-28 13:22:54" itemprop="dateModified" datetime="2019-07-28T13:22:54+08:00">2019-07-28</time>
              </span>
            
          

          

          
          

          
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
                 阅读次数： 
                <span class="busuanzi-value" id="busuanzi_value_page_pv"></span>
              </span>
            </span>
          

          

          <br>
          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>童年中的趣事是最最珍贵的。小时候，我们因为幼小无知而闹出了许多笑话，打开记忆的匣门，童年趣事便接踵而来。</p>
<p>做梦找到厕所，醒来尿床了</p>
<p>小时候穿着裆裤时，跟着小女孩跳皮筋</p>
<p>撒尿和泥，还玩的不亦乐乎</p>
<p>纠结自己要上清华还是北大，长大后发现想多了</p>
<a id="more"></a>
<p>用笔在自己胳膊上画手表，还舍不得洗掉</p>
<p>用放大镜烤蚂蚁（小时候居然那么残忍……）</p>
<p>老爸骑28自行车带我上街，忘了我已经先坐在后座上，偏腿上车，我就飞了出去…</p>
<p>听着这么一段小故事，或许都发现了这些虎事很有趣。</p>
<h3 id="功能展示"><a href="#功能展示" class="headerlink" title="功能展示"></a>功能展示</h3><ol>
<li>首页-视频播放 （同时只能播放一个视频特性）</li>
<li>点赞</li>
<li>分享</li>
<li>回复 （二级页面尚未完善）</li>
</ol>
<h3 id="知识总结"><a href="#知识总结" class="headerlink" title="知识总结"></a>知识总结</h3><h5 id="1-首页-视频播放-（实现统一时刻只能播放一个视频）"><a href="#1-首页-视频播放-（实现统一时刻只能播放一个视频）" class="headerlink" title="1. 首页-视频播放 （实现统一时刻只能播放一个视频）"></a>1. 首页-视频播放 （实现统一时刻只能播放一个视频）</h5><p>解决思路：无非就是找一个标识，作为全局变量去记录上次播放的视频。也可以使用循环的方式去实现，不过循环的方式非常的不友好，随着分页数据的加载回导致程序越来越慢！</p>
<p>方案实时：我使用的是** vuex *<em>,不过也可以采取 *</em> Storage ** 本地存储的方式。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">handleVideoPlay() &#123;</span><br><span class="line">        <span class="keyword">if</span> (!<span class="keyword">this</span>.playFlag) &#123;</span><br><span class="line">          <span class="keyword">this</span>.ajaxVideoClick()</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">this</span>.playFlag = <span class="literal">true</span>;</span><br><span class="line">        <span class="keyword">const</span> videoCtx = wx.createVideoContext(<span class="string">'video-'</span>+<span class="keyword">this</span>.video.id)</span><br><span class="line">        videoCtx.play()</span><br><span class="line">        <span class="comment">//用store定义的变量去判断 </span></span><br><span class="line">        <span class="keyword">if</span>(<span class="keyword">this</span>.videoPlayId === <span class="number">-1</span>)&#123;</span><br><span class="line">          <span class="comment">// 调用，修改 store中的 videoPlayId 值</span></span><br><span class="line">          <span class="keyword">this</span>[VideoPlayId](<span class="keyword">this</span>.video.id)</span><br><span class="line">          <span class="keyword">return</span> <span class="literal">false</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">const</span> videoCtx1 = wx.createVideoContext(<span class="string">'video-'</span>+<span class="keyword">this</span>.videoPlayId)</span><br><span class="line">        videoCtx1.pause()</span><br><span class="line">        <span class="keyword">this</span>[VideoPlayId](<span class="keyword">this</span>.video.id)</span><br><span class="line">        <span class="comment">// videoCtx.requestFullScreen()</span></span><br><span class="line">      &#125;,</span><br></pre></td></tr></table></figure>

<h5 id="2-分享功能"><a href="#2-分享功能" class="headerlink" title="2. 分享功能"></a>2. 分享功能</h5><p>小程序如果想对外分享，必须在 page 里面定义 onShareAppMessage 函数，来配置页面分享转发相关的信息。</p>
<ul>
<li>只有定义了此事件处理函数，右上角菜单才会显示 “转发” 按钮</li>
<li>用户点击转发按钮的时候会调用</li>
<li>此事件需要 return 一个 Object，用于自定义转发内容</li>
</ul>
<p>一个页面可能会有多个分享，可以由插入的参数options来判断具体是由哪个位置进行分享，从而做不同的逻辑判断。</p>
<p>不过在mpvue中，需要把 onShareAppMessage  函数写在 export  default { } 根节点中，与 methods方法通级。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">onShareAppMessage(res) &#123;</span><br><span class="line">       <span class="keyword">return</span> &#123;</span><br><span class="line">         title: <span class="string">'虎事小程序-'</span> + <span class="keyword">this</span>.video.auther,</span><br><span class="line">         path: <span class="keyword">this</span>.detaiUrl,</span><br><span class="line">         imageUrl: <span class="keyword">this</span>.video.img,</span><br><span class="line">         success: <span class="function"><span class="keyword">function</span> (<span class="params">shareTickets</span>) </span>&#123;</span><br><span class="line">           <span class="built_in">console</span>.info(shareTickets + <span class="string">'成功'</span>);</span><br><span class="line">           <span class="comment">// 转发成功</span></span><br><span class="line">         &#125;,</span><br><span class="line">         fail: <span class="function"><span class="keyword">function</span> (<span class="params">res</span>) </span>&#123;</span><br><span class="line">           <span class="built_in">console</span>.log(res + <span class="string">'失败'</span>);</span><br><span class="line">           <span class="comment">// 转发失败</span></span><br><span class="line">         &#125;,</span><br><span class="line">         complete:<span class="function"><span class="keyword">function</span>(<span class="params">res</span>)</span>&#123;</span><br><span class="line">           <span class="comment">// 不管成功失败都会执行</span></span><br><span class="line">         &#125;</span><br><span class="line">       &#125;</span><br><span class="line">     &#125;</span><br></pre></td></tr></table></figure>

<h5 id="3-带参数跳转"><a href="#3-带参数跳转" class="headerlink" title="3. 带参数跳转"></a>3. 带参数跳转</h5><p>在微信小程序中，跳转有普通导航跳转和tabar跳转。在mpvue框架中进行跳转可以使用 ** a 标签  *<em>去实现，也可以使用 *</em> wx.navigateTo()  **。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 跳转</span></span><br><span class="line">wx.navigateTo(&#123; <span class="attr">url</span>: <span class="string">'pages/detail/main?id'</span>+<span class="keyword">this</span>.video.id&#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 目的页面，参数的接收</span></span><br><span class="line"><span class="keyword">this</span>.videoId = <span class="keyword">this</span>.$root.$mp.query.id</span><br></pre></td></tr></table></figure>

<h5 id="4-使用-scss-书写css"><a href="#4-使用-scss-书写css" class="headerlink" title="4. 使用 scss 书写css"></a>4. 使用 scss 书写css</h5><p>首先需要安装node-scss 和 scss-loader</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">npm install scss-loader --save-dev</span><br><span class="line">npm install node-scss --save-dev</span><br></pre></td></tr></table></figure>

<p>安装完毕就可以这样书写css代码了</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">style</span> <span class="attr">lang</span>=<span class="string">'scss'</span> <span class="attr">scoped</span>&gt;</span></span><br><span class="line">  .video &#123;</span><br><span class="line">    margin-bottom: 10px;</span><br><span class="line">    overflow: hidden;</span><br><span class="line">    .video-img &#123;</span><br><span class="line">      overflow: hidden;</span><br><span class="line">      width: 100%;</span><br><span class="line">    &#125;</span><br><span class="line"> &#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>如果安装完，** <style lang="scss" scoped></style> ** 嵌套的代码，不智能提示，及 ** {}** 有红色波浪线警告，可以采用如下解决方案。</p>
<p>在VsCode编辑器中，点击 文件-首选项-设置 增加一下几行配置即可。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&quot;files.associations&quot;: &#123;</span><br><span class="line">	&quot;*.css&quot;: &quot;scss&quot;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h5 id="5-mpvue中如何使用vuex"><a href="#5-mpvue中如何使用vuex" class="headerlink" title="5.mpvue中如何使用vuex"></a>5.mpvue中如何使用vuex</h5><p>在pages根目录下创建stores的目录，并且新建 3个文件</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">index.js</span><br><span class="line">mutation-types.js</span><br><span class="line">mutations.js</span><br></pre></td></tr></table></figure>

<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// mutation-types.js</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 获取上次播放ID 用于结束上次播放的视频</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> VideoPlayId = <span class="string">'VideoPlayId'</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 获取微信用户信息 存有 openID</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> UserInfor = <span class="string">'UserInfor'</span></span><br></pre></td></tr></table></figure>

<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// mutations.js</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> &#123; VideoPlayId, UserInfor &#125; <span class="keyword">from</span> <span class="string">'./mutation-types'</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">    [VideoPlayId](state, v) &#123;</span><br><span class="line">        state.videoPlayId = v</span><br><span class="line">    &#125;,</span><br><span class="line">    [UserInfor](state, v) &#123;</span><br><span class="line">        state.userInfo = v</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// index.js</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> Vue <span class="keyword">from</span> <span class="string">'vue'</span></span><br><span class="line"><span class="keyword">import</span> Vuex <span class="keyword">from</span> <span class="string">'vuex'</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> mutations <span class="keyword">from</span> <span class="string">'./mutations'</span></span><br><span class="line"></span><br><span class="line">Vue.use(Vuex)</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> state = &#123;</span><br><span class="line">    videoPlayId: <span class="number">-1</span>,</span><br><span class="line">    userInfo: &#123;&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="keyword">new</span> Vuex.Store(&#123;</span><br><span class="line">    state,</span><br><span class="line">    mutations</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<p>绑定Store 到mpvue</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> Vue <span class="keyword">from</span> <span class="string">'vue'</span></span><br><span class="line"><span class="keyword">import</span> App <span class="keyword">from</span> <span class="string">'./App'</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 导入Store</span></span><br><span class="line"><span class="keyword">import</span> Store <span class="keyword">from</span> <span class="string">'./stores'</span></span><br><span class="line"></span><br><span class="line">Vue.config.productionTip = <span class="literal">false</span></span><br><span class="line">App.mpType = <span class="string">'app'</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 绑定</span></span><br><span class="line">Vue.prototype.$store = Store</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> app = <span class="keyword">new</span> Vue(App)</span><br><span class="line">app.$mount()</span><br></pre></td></tr></table></figure>

<p>在使用的地方，需要吧state定义的变量，及触发的方法导出。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">  &lt;div&gt;</span><br><span class="line">    &lt;button :<span class="class"><span class="keyword">class</span></span>=<span class="string">"&#123;logged: isLogged&#125;"</span> </span><br><span class="line">			open-type=<span class="string">"getUserInfo"</span> :lang=<span class="string">"zh_CN"</span> </span><br><span class="line">			@click=<span class="string">"doLogin"</span></span><br><span class="line">	&gt;获取用户信息&lt;<span class="regexp">/button&gt;</span></span><br><span class="line"><span class="regexp">  &lt;/</span>div&gt;</span><br><span class="line">&lt;<span class="regexp">/template&gt;</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">&lt;script&gt;</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">/</span><span class="regexp">/ ---开始 vuex </span></span><br><span class="line"><span class="regexp">import &#123; mapState, mapMutations &#125; from 'vuex'</span></span><br><span class="line"><span class="regexp">import &#123; UserInfor &#125; from '@/</span>stores/mutation-types<span class="string">'</span></span><br><span class="line"><span class="string">// ---结束 vuex</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">import &#123; get, showSuccess, showModal &#125; from '</span>@/utils/index<span class="string">'</span></span><br><span class="line"><span class="string">import qcloud from '</span>wafer2-client-sdk<span class="string">'</span></span><br><span class="line"><span class="string">import config from '</span>./../../config<span class="string">'</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">export default &#123;</span></span><br><span class="line"><span class="string">  data () &#123;</span></span><br><span class="line"><span class="string">    return &#123;</span></span><br><span class="line"><span class="string">    &#125;</span></span><br><span class="line"><span class="string">  &#125;,</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">  computed:&#123;</span></span><br><span class="line"><span class="string">    // --- 导出 state 定义的 userInfo 变量</span></span><br><span class="line"><span class="string">    ...mapState([</span></span><br><span class="line"><span class="string">      '</span>userInfo<span class="string">'</span></span><br><span class="line"><span class="string">    ]),</span></span><br><span class="line"><span class="string">    isLogged () &#123;</span></span><br><span class="line"><span class="string">      return !!this.userInfo['</span>openId<span class="string">'];</span></span><br><span class="line"><span class="string">    &#125;</span></span><br><span class="line"><span class="string">  &#125;,</span></span><br><span class="line"><span class="string">  methods: &#123;</span></span><br><span class="line"><span class="string">    /* --- 导出 Mutations 定义的 UserInfor变量用来，</span></span><br><span class="line"><span class="string">		   修改Store.state的userInfo值 */</span></span><br><span class="line"><span class="string">    ...mapMutations([</span></span><br><span class="line"><span class="string">      UserInfor</span></span><br><span class="line"><span class="string">    ]),</span></span><br><span class="line"><span class="string">    doLogin()&#123;</span></span><br><span class="line"><span class="string">      qcloud.login(&#123;</span></span><br><span class="line"><span class="string">        success: res =&gt; &#123;</span></span><br><span class="line"><span class="string">		  // 调用Store 方法去修改</span></span><br><span class="line"><span class="string">          this[UserInfor](res)</span></span><br><span class="line"><span class="string">          // this.userInfo // 可以 直接访问</span></span><br><span class="line"><span class="string">        &#125;,</span></span><br><span class="line"><span class="string">        fail: err =&gt; &#123;</span></span><br><span class="line"><span class="string">          this[UserInfor](&#123;&#125;)</span></span><br><span class="line"><span class="string">          showModal('</span>登录错误<span class="string">')</span></span><br><span class="line"><span class="string">        &#125;</span></span><br><span class="line"><span class="string">      &#125;)</span></span><br><span class="line"><span class="string">    &#125;</span></span><br><span class="line"><span class="string">  &#125;</span></span><br><span class="line"><span class="string">&#125;</span></span><br><span class="line"><span class="string">&lt;/script&gt;</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">&lt;style scoped&gt;</span></span><br><span class="line"><span class="string">.logged&#123;</span></span><br><span class="line"><span class="string">  display: none;</span></span><br><span class="line"><span class="string">&#125;</span></span><br><span class="line"><span class="string">&lt;/style&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="作品展示"><a href="#作品展示" class="headerlink" title="作品展示"></a>作品展示</h3><img src="/posts/88e91fb0/1.png" width="260" title="点赞页面">
<img src="/posts/88e91fb0/2.png" width="260" title="播放页">
<img src="/posts/88e91fb0/3.png" width="260" title="已点赞页面">
<img src="/posts/88e91fb0/4.png" width="260" title="首页">
<img src="/posts/88e91fb0/5.png" width="260" title="分享页面">

      
    </div>

    
      


    

    
      
    

    
    
    

    

    
      
    
    

    
      <div>
        




  



<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>修心</li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    
    <a href="https://blog.youngzk.com/posts/88e91fb0/" title="虎事小程序">https://blog.youngzk.com/posts/88e91fb0/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/zh-CN" rel="noopener" target="_blank"><i class="fa fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！</li>
</ul>

      </div>
    

    <footer class="post-footer">
      
        
          
        
        <div class="post-tags">
          
            <a href="/tags/微信小程序/" rel="tag"># 微信小程序</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/posts/a4e34216/" rel="next" title="七夕之日">
                <i class="fa fa-chevron-left"></i> 七夕之日
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/posts/9f19e967/" rel="prev" title="typeScript">
                typeScript <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>

  </div>


          </div>
          
  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-overview">

          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  
    <a href="/">
      <img class="site-author-image" itemprop="image" src="/images/avatar.jpg" alt="修心">
      </a>
  
  <p class="site-author-name" itemprop="name">修心</p>
  <div class="site-description motion-element" itemprop="description">修不被别人影响的心，修不被自己控制的心，修不被别人诱惑的心。</div>
</div>


  <nav class="site-state motion-element">
    
      <div class="site-state-item site-state-posts">
        
          <a href="/archives/">
        
          <span class="site-state-item-count">17</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
    

    
      
      
      <div class="site-state-item site-state-categories">
        
          
            <a href="/categories/">
          
        
        
        
          
        
          
        
          
        
        <span class="site-state-item-count">3</span>
        <span class="site-state-item-name">分类</span>
        </a>
      </div>
    

    
      
      
      <div class="site-state-item site-state-tags">
        
          
            <a href="/tags/">
          
        
        
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
        <span class="site-state-item-count">20</span>
        <span class="site-state-item-name">标签</span>
        </a>
      </div>
    
  </nav>







  <div class="links-of-author motion-element">
    
      <span class="links-of-author-item">
      
      
        
      
      
        
      
        <a href="https://github.com/1160007652" title="GitHub &rarr; https://github.com/1160007652" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>GitHub</a>
      </span>
    
      <span class="links-of-author-item">
      
      
        
      
      
        
      
        <a href="mailto:1160007652@qq.com" title="E-Mail &rarr; mailto:1160007652@qq.com" rel="noopener" target="_blank"><i class="fa fa-fw fa-envelope"></i>E-Mail</a>
      </span>
    
  </div>



  <div class="cc-license motion-element" itemprop="license">
  
  
    
  
  
  
    <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/zh-CN" class="cc-opacity" rel="noopener" target="_blank"><img src="/images/cc-by-nc-sa.svg" alt="Creative Commons"></a>
  </div>





          
          
        </div>
      </div>

      
      <!--noindex-->
        <div class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
            
            
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#功能展示"><span class="nav-number">1.</span> <span class="nav-text">功能展示</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#知识总结"><span class="nav-number">2.</span> <span class="nav-text">知识总结</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#1-首页-视频播放-（实现统一时刻只能播放一个视频）"><span class="nav-number">2.0.1.</span> <span class="nav-text">1. 首页-视频播放 （实现统一时刻只能播放一个视频）</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#2-分享功能"><span class="nav-number">2.0.2.</span> <span class="nav-text">2. 分享功能</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#3-带参数跳转"><span class="nav-number">2.0.3.</span> <span class="nav-text">3. 带参数跳转</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#4-使用-scss-书写css"><span class="nav-number">2.0.4.</span> <span class="nav-text">4. 使用 scss 书写css</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#5-mpvue中如何使用vuex"><span class="nav-number">2.0.5.</span> <span class="nav-text">5.mpvue中如何使用vuex</span></a></li></ol></li></ol><li class="nav-item nav-level-3"><a class="nav-link" href="#作品展示"><span class="nav-number">3.</span> <span class="nav-text">作品展示</span></a></li></div>
            

          </div>
        </div>
      <!--/noindex-->
      

      

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; 2018 – <span itemprop="copyrightYear">2019</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">修心</span>

  

  
</div>

<div>
  修不被别人影响的心，修不被自己控制的心，修不被别人诱惑的心。
</div>









        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="post-meta-item-icon">
      <i class="fa fa-user"></i>
    </span>
    <span class="site-uv" title="总访客量">
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
    </span>
  

  
    <span class="post-meta-divider">|</span>
  

  
    <span class="post-meta-item-icon">
      <i class="fa fa-eye"></i>
    </span>
    <span class="site-pv" title="总访问量">
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
    </span>
  
</div>









        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
          <span id="scrollpercent"><span>0</span>%</span>
        
      </div>
    

    

    

    

  </div>

  

<script>
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>






  











  
  









  
  <script src="/lib/jquery/index.js?v=3.4.1"></script>

  
  <script src="/lib/fancybox/source/jquery.fancybox.pack.js"></script>

  
  <script src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>




  <script src="/js/utils.js?v=7.2.0"></script>

  <script src="/js/motion.js?v=7.2.0"></script>



  
  


  <script src="/js/affix.js?v=7.2.0"></script>

  <script src="/js/schemes/pisces.js?v=7.2.0"></script>



  
  <script src="/js/scrollspy.js?v=7.2.0"></script>
<script src="/js/post-details.js?v=7.2.0"></script>



  <script src="/js/next-boot.js?v=7.2.0"></script>

  

  

  


  













  <script src="/js/local-search.js?v=7.2.0"></script>












<script>
// GET RESPONSIVE HEIGHT PASSED FROM IFRAME

window.addEventListener("message", function(e) {
  var data = e.data;
  if ((typeof data === 'string') && (data.indexOf('ciu_embed') > -1)) {
    var featureID = data.split(':')[1];
    var height = data.split(':')[2];
    $(`iframe[data-feature=${featureID}]`).height(parseInt(height) + 30);
  }
}, false);
</script>








  

<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"jsonPath":"/live2dw/assets/hijiki.model.json"},"display":{"position":"right","width":150,"height":300},"mobile":{"show":true},"react":{"opacity":0.7},"log":false});</script></body>
</html>
